package com.xiaoxu.springboot.modules.test.controller;

import com.xiaoxu.springboot.modules.common.vo.Result;
import com.xiaoxu.springboot.modules.common.vo.Search;
import com.xiaoxu.springboot.modules.test.entity.Student;
import com.xiaoxu.springboot.modules.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**http://localhost:8183/api/student/insert
     *
     * @param student
     * @return
     */
    @PostMapping(value = "/student/insert" ,consumes = "application/json")
    public Result<Student> insertStudent(@RequestBody Student student){
        return studentService.insertStudent(student);
    }

    /**
     *http://localhost:8183/api/student/update
     * @param student
     * @return
     */
   @PostMapping(value = "/student/update",consumes = "application/json")
    public Result<Student> updateStudent(@RequestBody Student student){
        return studentService.updateStudent(student);
    }

    /**
     *http://localhost:8183/api/student/del/5
     * @param id
     * @return
     */
    @DeleteMapping(value = "/student/del/{id}")
    public  Result<Object> deleteStudentById(@PathVariable Integer id){
        return studentService.deleteStudentById(id);
    }

    /**
     * http://localhost:8183/api/student/s1/8
     * @param id
     * @return
     */
    @GetMapping("/student/s1/{id}")
    public Student getStudentById(@PathVariable Integer id){
        return studentService.getStudentById(id);
    }

    /**
     * http://localhost:8183/api/student/s1
     * {"currentPage":1,"pageSize":5,"sort":"studentName","direction":"desc","keyword":""}
     * @param search
     * @return
     */
    @PostMapping(value = "/student/s2",consumes = "application/json")
    public Page selectByseach(@RequestBody Search search){
        return studentService.selectByseach(search);
    }

    /**
     * 127.0.0.1/api/student/s3?studentName=xiaoxu ---- get
     */
    @GetMapping("/student/s3")
    public List<Student> getStudentsByStudentName(@RequestParam String studentName) {
        return studentService.getStudentsByStudentName(studentName);
    }


}
